package edu.gznu.ics.fast.pulsar.service.candMatchService.impl;

import edu.gznu.ics.fast.pulsar.dao.prestoacc.pic.PicInfoMapper;
import edu.gznu.ics.fast.pulsar.dao.prestoacc.knowPulsar.KnownPulsarCandidateMapper;
import edu.gznu.ics.fast.pulsar.model.po.prestoacc.pic.PicInfoPO;
import edu.gznu.ics.fast.pulsar.model.vo.prestoacc.knowPulsar.KnownPulsarCandidateVO;
import edu.gznu.ics.fast.pulsar.service.candMatchService.PulsarMatchCandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by fei-zyx on 2016/11/10.
 */
@Service("candidateCompareService")
public class PulsarMatchCandServiceImply implements PulsarMatchCandService {
    /*public static  void main(String []argv){


    }*/
    @Autowired
    KnownPulsarCandidateMapper knownPulsarCandidateMapper;
    @Autowired
    PicInfoMapper picInfoMapper;

    public List<KnownPulsarCandidateVO> findKonwPulsarList() {
        return knownPulsarCandidateMapper.queryKnowPulsarCandList();
    }

    public List<KnownPulsarCandidateVO> findKonwPulsarListre() {
        return knownPulsarCandidateMapper.queryKnowPulsarCandListre();
    }

    public List<PicInfoPO> findPicInfoByKnownPulsar(KnownPulsarCandidateVO knownPulsarCandidateVO) {
        return picInfoMapper.queryPicInfoPeriodByKnownPulsarPeriod(knownPulsarCandidateVO);
    }

    public List<PicInfoPO> matchPicInfoByPicInfoLists(KnownPulsarCandidateVO knownPulsarCandidateVO) {
        List<KnownPulsarCandidateVO> knownPulsarCandidateVO1 = knownPulsarCandidateMapper.queryKnowPulsarCandList();

        // for (int k = 0; k < knownPulsarCandidateVO1.size(); k++) {

        List<PicInfoPO> picInfoPOs = findPicInfoByKnownPulsar(knownPulsarCandidateVO);
        List<PicInfoPO> repicInfoPOs = new LinkedList<PicInfoPO>();
        for (int i = 1; i < 256; i++) {
            BigDecimal dividNum = new BigDecimal(i).subtract((new BigDecimal(((int) i / 16)).add(new BigDecimal(0))).multiply(new BigDecimal(16))).divide((new BigDecimal(((int) i / 16)).add(new BigDecimal(1))), 10, BigDecimal.ROUND_HALF_DOWN);
            BigDecimal periodM = knownPulsarCandidateVO.getPulsarPeriod().multiply(dividNum);
            if (dividNum.compareTo(new BigDecimal(0)) != 0) {
                for (int j = 0; j < picInfoPOs.size(); j++) {
                    PicInfoPO picInfoPO = picInfoPOs.get(j);
                    BigDecimal ptopo = new BigDecimal(picInfoPO.getPtopo());
                    BigDecimal d = ptopo.subtract(periodM).abs().divide(ptopo, 10, BigDecimal.ROUND_HALF_DOWN);
                    if (d.abs().compareTo(new BigDecimal(0.001)) - 1 < 0) {
                        picInfoPO.setCoefficient(dividNum);
                        repicInfoPOs.add(picInfoPO);
                        picInfoPOs.remove(j);
                    }

                }
            }
        }
        int size = repicInfoPOs.size();
        System.out.print(size + "=========================================");
        //knownPulsarCandidateVO.setMatchCount(size);
        // knownPulsarCandidateMapper.updatePicMatchCount(knownPulsarCandidateVO);

        return repicInfoPOs;
        // }return null;
    }

    @Override
    public List<KnownPulsarCandidateVO> candMatchPulsar() {


        return null;
    }

}
